package com.guodi.model.service.impl;

import com.guodi.model.service.IRedisDictService;
import com.guodi.system.feign.IDictClient;
import com.guodi.system.vo.DictVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("redisDictServiceImpl1")
public class RedisDictServiceImpl implements IRedisDictService {

    @Resource
    IDictClient dictBizClient;

    @Override
    public String getText(String code, String dictKey) {
        String result = null;
        if (dictBizClient.getValue(code, dictKey) != null && dictBizClient.getValue(code, dictKey).getCode() == 200) {
            result = dictBizClient.getValue(code, dictKey).getData();
        }
        return result;
    }

    @Override
    public List<Map<String, String>> getItems(String code) {
        List<Map<String, String>> redisList = new ArrayList<>();
        if (dictBizClient.getList(code) != null && dictBizClient.getList(code).getCode() == 200) {
            List<DictVO> resulitList = dictBizClient.getList(code).getData();
            if (resulitList != null && resulitList.size() > 0) {
                for (DictVO dict : resulitList) {
                    Map<String, String> dictMap = new HashMap<>();
                    dictMap.put("text", dict.getDictValue());
                    dictMap.put("code", dict.getDictKey());
                    redisList.add(dictMap);
                }
            }
        }
        return redisList;
    }

    @Override
    public Map<String, String> getItemMaps(String code) {
        Map<String, String> redisMap = new HashMap<>();
        if (dictBizClient.getList(code) != null && dictBizClient.getList(code).getCode() == 200) {
            List<DictVO> resulitList = dictBizClient.getList(code).getData();
            if (resulitList != null && resulitList.size() > 0) {
                for (DictVO dict : resulitList) {
                    redisMap.put(dict.getDictKey(), dict.getDictValue());
                }
            }
        }
        return redisMap;
    }

    @Override
    public Map<String, String> getItemNameMaps(String code) {
        Map<String, String> redisMap = new HashMap<>();
        if (dictBizClient.getList(code) != null && dictBizClient.getList(code).getCode() == 200) {
            List<DictVO> resulitList = dictBizClient.getList(code).getData();
            if (resulitList != null && resulitList.size() > 0) {
                for (DictVO dict : resulitList) {
                    redisMap.put(dict.getDictValue(), dict.getDictKey());
                }
            }
        }
        return redisMap;
    }
}
